var uid,cur_page;
var iteam = '#itemA';
var item_num = 1;
var notes = [], carousel=[];
var app = angular.module("homeApp",[]);
app.controller("homeController",function($scope,$http){
	/**社区首页数据*/
	$scope.initNote = function(){
		plus.nativeUI.showWaiting('加载中...');  
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.index',
				cid:item_num
			}
		}).then(function successCallback(response){	
			var info = response.data.data;
			$scope.item_num = item_num;
			$scope.new = info.new[item_num];//标题下的数据
			$scope.hot = info.hot[item_num];//标题下的数据
			$scope.photos = info.photo[item_num];//轮播图  
			$scope.cates = info.cates[item_num];  
			plus.nativeUI.closeWaiting();
		},function errorfunction(e){
			console.log(JSON.stringify(e));
			plus.nativeUI.closeWaiting();				
		});
	}
	
	//专题
	$scope.special_topic = function(){
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:"Community.getSpecialInfo",					
			}
		}).then(function successFunction(result){
			$scope.show_topic = 1;
			$scope.topic_info = result.data.data;
			var mySwiper = new Swiper('.swiper-container', {
				slidesPerView: 3,
				observer: true,//修改swiper自己或子元素时，自动初始化swiper
	            observeParents: true//修改swiper的父元素时，自动初始化swiper
			})
		},function errorFunction(e){
			console.log(JSON.stringify(e));  
		})
	}
	/**交易首页数据*/
	$scope.initTrade = function(){
		plus.nativeUI.showWaiting('加载中...')
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Trade.getGoods'
				}
		}).then(function successCallback(response){ 
			var info = response.data.data;
			$scope.carousels = info.carousel;  //轮播
			$scope.hots = info.hot;
			$scope.news = info.new;
			$scope.recommends = info.recommend;
			plus.nativeUI.closeWaiting();
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();		
			console.log(e);
		})
	}		
	
	$scope.open_special_topic = function(statu,spe_title){
		plus.webview.create('special_topic.html', 'special_topic.html',{},{typeId:statu,topic_title:spe_title}).show('pop-in');
	}
	
	//商品详情
	$scope.obtainId = function(uid,type,id){
		plus.webview.create('goods_detail.html', 'goods_detail.html',{},{gUid:uid,type:type,gId:id}).show('pop-in');          
	}
	
	//图文内容详情
	$scope.shequ=function(uid,type,id){
		plus.webview.create('view_detail.html', 'view_detail.html',{hardwareAccelerated:true},{noteUid:uid,type:type,noteId:id}).show('pop-in'); 
	}
	
	//最新、热门内容
	$scope.shequNote =function(cid,nh){
		plus.webview.create('new_content.html', 'new_content.html',{},{cid:cid,is_hot:nh}).show('pop-in');
	}
	//推荐、最新、热门商品
    $scope.tradeNew = function(type,title){
    	plus.webview.create('new_goods.html', 'new_goods.html',{},{state:type,title:title}).show('pop-in'); 
    }
})






document.addEventListener("plusready", function() {
	uid = plus.storage.getItem('uid');
	appElement = document.querySelector('[ng-controller=homeController]');
	$scope = angular.element(appElement).scope();  
	//执行一次
	var is_first = true;
	if(is_first){
		$scope.initNote();
		is_first = false;
	}
	$scope.special_topic();  //专题  
	//交易
	$('#bussiness').on('tap',function(){
		$(this).addClass('mui-active');
		$scope.initTrade();
	})
	$scope.$apply();
})
       